using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SkeletonBattleState : EnemyState
{
    EnemySkeleton enemy;
    private Transform player;
    private int moveDir;
    public SkeletonBattleState(Enemy _enemyBase, EnemyStateMachine _statemachine, string _animBoolName, EnemySkeleton enemy) : base(_enemyBase, _statemachine, _animBoolName)
    {
        this.enemy = enemy;
    }

    public override void Enter()
    {
        base.Enter();
        player = PlayerManager.instance.player.transform;
    }

    public override void Exit()
    {
        base.Exit();
    }


    public override void Update()
    {
        base.Update();
        if (enemy.IsPlayerDetected()|| Vector2.Distance(enemy.transform.position, player.transform.position) < 5)
        {
            stateTimer = enemy.battleTime;
            if (enemy.IsPlayerDetected() && enemy.IsPlayerDetected().distance < enemy.attackDistance && canAtack())
            {
                enemy.SetVlocitityZero();
                stateMachine.ChangeState(enemy.attackState);
            }
        }
        else
        {
            if (stateTimer < 0|| Vector2.Distance(player.transform.position,enemy.transform.position)>10)
            {
                stateMachine.ChangeState(enemy.idelState);
            }
        }


        if (player.position.x > enemy.transform.position.x)
        {
            moveDir = 1;
        }
        else
        {
            moveDir = -1;
        }
        enemy.SetVelocity(enemy.moveSpeed * moveDir, rb.velocity.y);
    }
    private bool canAtack()
    {
        if (Time.time > enemy.lastTimeAttack + enemy.attackCooldown)
        {
            enemy.lastTimeAttack = Time.time;
            return true;
        }
        else
            return false;
    }
}
